package com.xigege.netty.TCPStickyBag.solve.presetLength;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author huangxi
 * @class StickHalfBagServer
 * @description StickHalfBagServer
 * @create 2022-08-23 15:03
 */
public class PresetLengthServer {
    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup(2);
        try {
            ChannelFuture future = new ServerBootstrap()
                    .group(boss, worker)
                    .channel(NioServerSocketChannel.class)
//                    .childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(16,16,16)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            // LengthFieldBasedFrameDecoder构造函数各参数含义
                            //   lengthFieldOffset：记录长度所在的偏移量，
                            //   lengthFieldLength：记录长度的字节数
                            //   lengthAdjustment：记录长度的位置之后还有几个字节是真正的数据
                            //   initialBytesToStrip：从头开始，剥离几个字节不要

                            // ex: (源码里的例子)
                            //    lengthFieldOffset   = 1 (= the length of HDR1)
                            //    lengthFieldLength   = 2
                            //    lengthAdjustment    = 1 (= the length of HDR2)
                            //    initialBytesToStrip = 3 (= the length of HDR1 + LEN)
                            //
                            //    BEFORE DECODE (16 bytes)                       AFTER DECODE (13 bytes)
                            //    +------+--------+------+----------------+      +------+----------------+
                            //    | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
                            //    | 0xCA | 0x000C | 0xFE | "HELLO, WORLD" |      | 0xFE | "HELLO, WORLD" |
                            //    +------+--------+------+----------------+      +------+----------------+
                            ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 4, 8));
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        }
                    }).bind(6666).sync();
            Channel channel = future.channel();

            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }
}
